<?php

namespace App\Http\Controllers\Test;

use App\Http\Controllers\Controller;
use App\Models\Article;
use Illuminate\Support\Facades\DB;


class OptimizeController extends Controller
{
    // mysql 数据库碎片优化
    public function initialize()
    {
        parent::initialize();
    }

    // 测试
    public function optimize()
    {
        // 查询所有的数据表
        $cline   = chr(10);
        $db_name = 'laravel_slh';
        $db_sql  = "select TABLE_NAME from information_schema.tables where table_schema='".$db_name."';";
        $db_info = DB::select($db_sql);
        if(empty($db_info)){
            $this->error = '数据库不存在！！';

            return false;
        }

        ## 所有数据表
        $tabels_sql = "select table_name from information_schema.tables where table_schema='ctsnews' and table_type='base table';";
        $tabel_list = DB::select($tabels_sql);

        // 执行优化数据表碎片空间操作
        foreach($tabel_list as $table){
            $_table = $table->table_name;
            $_res   = DB::statement("optimize table `$db_name`.`$_table` ");
            //echo ' == 优化数据表空间：'.$_table.$cline;
        }

        return $this->success($_res);
    }

}


